/*
 *  Copyright 2009 Allan Lykke Christensen.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package dk.i2m.netbeans.modules.doxia.lexer;

import dk.i2m.netbeans.modules.doxia.AptDataLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.netbeans.spi.lexer.LanguageHierarchy;
import org.netbeans.spi.lexer.Lexer;
import org.netbeans.spi.lexer.LexerRestartInfo;

/**
 * Hierarchy of the Apt language.
 *
 * @author Allan Lykke Christensen
 */
public class AptLanguageHierarchy extends LanguageHierarchy<AptTokenId> {

    private static List<AptTokenId> tokens;
    private static Map<Integer, AptTokenId> idToToken;

    private static void init() {
        tokens = new ArrayList<AptTokenId>();
        for (AptToken aptToken : AptToken.values()) {
            tokens.add(new AptTokenId(aptToken));
        }
        idToToken = new HashMap<Integer, AptTokenId>();

        for (AptTokenId token : tokens) {
            idToToken.put(token.ordinal(), token);
        }
    }

    static synchronized AptTokenId getToken(
            int id) {
        if (idToToken == null) {
            init();
        }

        return idToToken.get(id);
    }

    @Override
    protected Collection<AptTokenId> createTokenIds() {
        if (tokens == null) {
            init();
        }
        return tokens;
    }

    @Override
    protected Lexer<AptTokenId> createLexer(LexerRestartInfo<AptTokenId> info) {
        return new AptLexer(info);
    }

    @Override
    protected String mimeType() {
        return AptDataLoader.REQUIRED_MIME;
    }
}
